<template>
  <div style="padding: 50px">
    <el-form
      :model="form"
      ref="form"
      label-width="100px"
      style="width: 80%; max-width: 900px"
    >
      <el-form-item
        label="考生号"
        label-position="left"
        prop="id"
        :rules="[{ required: true, message: '请输入考生号', trigger: 'blur' }]"
      >
        <el-input v-model.number="form.id"></el-input>
      </el-form-item>
      <el-form-item
        label="性别"
        label-position="left"
        prop="sex"
        :rules="[{ required: true, message: '请选择性别', trigger: 'blur' }]"
      >
        <el-radio-group v-model="form.sex">
          <el-radio label="男">男</el-radio>
          <el-radio label="女">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="民族"
        label-position="left"
        prop="nation"
        :rules="[
          {
            required: true,
            message: '请填写民族，格式为xx族',
            trigger: 'blur',
          },
        ]"
      >
        <el-input v-model="form.nation"></el-input>
      </el-form-item>
      <el-form-item
        label="政治面貌"
        label-position="left"
        prop="politicsSt"
        :rules="[
          { required: true, message: '请填写政治面貌', trigger: 'blur' },
        ]"
      >
        <el-input v-model="form.politicsSt"></el-input>
      </el-form-item>
      <el-form-item
        label="院校名称"
        label-position="left"
        prop="school"
        :rules="[
          { required: true, message: '请填写院校名称', trigger: 'blur' },
        ]"
      >
        <el-input v-model="form.school"></el-input>
      </el-form-item>
      <el-form-item
        label="学历"
        label-position="left"
        prop="eduBackgro"
        :rules="[{ required: true, message: '请选择学历', trigger: 'blur' }]"
      >
        <el-radio-group v-model="form.eduBackgro">
          <el-radio label="本科生毕业">本科生毕业</el-radio>
          <el-radio label="研究生毕业">研究生毕业</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="专业"
        label-position="left"
        prop="major"
        :rules="[{ required: true, message: '请填写专业', trigger: 'blur' }]"
      >
        <el-input v-model="form.major"></el-input>
      </el-form-item>
      <el-form-item
        label="生源所在地"
        label-position="left"
        prop="origin"
        :rules="[
          { required: true, message: '请选择生源所在地', trigger: 'blur' },
        ]"
      >
        <el-cascader
          :options="options"
          v-model="form.origin"
          placeholder="请选择"
        >
        </el-cascader>
      </el-form-item>
      <el-form-item
        label="毕业时间"
        label-position="left"
        prop="gradTime"
        :rules="[
          {
            required: true,
            message: '请填写毕业年月，格式为yyyymm',
            trigger: 'blur',
          },
        ]"
      >
        <el-input v-model.number="form.gradTime"></el-input>
      </el-form-item>
      <el-form-item
        label="困难生类别"
        label-position="left"
        prop="difficulty"
        :rules="[
          { required: true, message: '请选择困难生类别', trigger: 'blur' },
        ]"
      >
        <el-radio-group v-model="form.difficulty">
          <el-radio label="家庭困难">家庭困难</el-radio>
          <el-radio label="非困难生">非困难生</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="所在院系"
        label-position="left"
        prop="department"
        :rules="[
          { required: true, message: '请填写所在院系', trigger: 'blur' },
        ]"
      >
        <el-input v-model="form.department"></el-input>
      </el-form-item>
      <el-form-item
        label="学号"
        label-position="left"
        prop="sid"
        :rules="[{ required: true, message: '请填写学号', trigger: 'blur' }]"
      >
        <el-input v-model.number="form.sid"></el-input>
      </el-form-item>
      <el-form-item
        label="毕业去向"
        label-position="left"
        prop="direction"
        :rules="[
          { required: true, message: '请填写毕业去向', trigger: 'blur' },
        ]"
      >
        <el-input v-model="form.direction"></el-input>
      </el-form-item>
      <el-form-item label="单位名称" label-position="left" prop="orgName">
        <el-input v-model="form.orgName"></el-input>
      </el-form-item>
      <el-form-item label="单位性质" label-position="left" prop="orgKind">
        <el-input v-model="form.orgKind"></el-input>
      </el-form-item>
      <el-form-item label="单位行业" label-position="left" prop="orgIndustry">
        <el-input v-model="form.orgIndustry"></el-input>
      </el-form-item>
      <el-form-item label="单位所在地" label-position="left" prop="orgLocation">
        <el-input v-model="form.orgLocation"></el-input>
      </el-form-item>
      <el-form-item label="工作职位类别" label-position="left" prop="jobType">
        <el-input v-model="form.jobType"></el-input>
      </el-form-item>
      <el-form-item label="单位地址" label-position="left" prop="orgAddress">
        <el-input v-model="form.orgAddress"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
        <el-button type="plain" @click="handleCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addGradDirection, editGradDirection } from '@/api/graduation.js'

export default {
  data() {
    return {
      isCreate: true,
      form: {
        id: "",
        sex: "男",
        nation: "",
        politicsSt: "",
        school: "",
        eduBackgro: "本科生毕业",
        major: "",
        origin: "",
        gradTime: "",
        difficulty: "非困难生",
        department: "",
        sid: "",
        direction: "",
        orgName: "",
        orgKind: "",
        orgIndustry: "",
        orgLocation: "",
        jobType: "",
        orgAddress: ""
      },
      options: [
        {
          value: "省份",
          label: "省份",
          children: [
            {
              value: "河北省",
              label: "河北省"
            }, {
              value: "山西省",
              label: "山西省"
            }, {
              value: "辽宁省",
              label: "辽宁省"
            }, {
              value: "吉林省",
              label: "吉林省"
            }, {
              value: "黑龙江省",
              label: "黑龙江省"
            }, {
              value: "江苏省",
              label: "江苏省"
            }, {
              value: "浙江省",
              label: "浙江省"
            }, {
              value: "安徽省",
              label: "安徽省"
            }, {
              value: "福建省",
              label: "福建省"
            }, {
              value: "江西省",
              label: "江西省"
            }, {
              value: "山东省",
              label: "山东省"
            }, {
              value: "河南省",
              label: "河南省"
            }, {
              value: "湖北省",
              label: "湖北省"
            }, {
              value: "湖南省",
              label: "湖南省"
            }, {
              value: "广东省",
              label: "广东省"
            }, {
              value: "海南省",
              label: "海南省"
            }, {
              value: "四川省",
              label: "四川省"
            }, {
              value: "贵州省",
              label: "贵州省"
            }, {
              value: "云南省",
              label: "云南省"
            }, {
              value: "陕西省",
              label: "陕西省"
            }, {
              value: "甘肃省",
              label: "甘肃省"
            }, {
              value: "青海省",
              label: "青海省"
            }, {
              value: "台湾省",
              label: "台湾省"
            }
          ]
        }, {
          value: "自治区",
          label: "自治区",
          children: [
            {
              value: "内蒙古自治区",
              label: "内蒙古自治区"
            }, {
              value: "广西壮族自治区",
              label: "广西壮族自治区"
            }, {
              value: "西藏自治区",
              label: "西藏自治区"
            }, {
              value: "宁夏回族自治区",
              label: "宁夏回族自治区"
            }, {
              value: "新疆维吾尔自治区",
              label: "新疆维吾尔自治区"
            },
          ]
        }, {
          value: "直辖市",
          label: "直辖市",
          children: [
            {
              value: "北京市",
              label: "北京市"
            }, {
              value: "天津市",
              label: "天津市"
            }, {
              value: "上海市",
              label: "上海市"
            }, {
              value: "重庆市",
              label: "重庆市"
            }
          ]
        }, {
          value: "特别行政区",
          label: "特别行政区",
          children: [{
            value: "香港特别行政区",
            label: "香港特别行政区",
          }, {
            value: "澳门特别行政区",
            label: "澳门特别行政区"
          }]
        }
      ]
    }
  },
  created() {
    let form = this.$route.params.dataObj
    console.log(form)
    if (form) {
      this.form = form
      this.isCreate = false;
    } else {
      this.isCreate = true;
    }
  },
  methods: {
    handleSubmit() {
      //判断是否符合要求
      this.$refs['form'].validate((valid) => {
        if (valid) {
          //上传给后端
          // alert('submit!');
          if (this.isCreate) {
            //新建上传
            let data = {
              id: this.form.id,
              sex: this.form.sex,
              nation: this.form.nation,
              politicsSt: this.form.politicsSt,
              school: this.form.school,
              eduBackgro: this.form.eduBackgro,
              major: this.form.major,
              origin: this.form.origin[1],
              gradTime: this.form.gradTime,
              difficulty: this.form.difficulty,
              department: this.form.difficulty,
              sid: this.form.sid,
              direction: this.form.direction,
              orgName: this.form.orgName,
              orgKind: this.form.orgKind,
              orgIndustr: this.form.orgIndustry,
              orgLocatio: this.form.orgLocation,
              jobType: this.form.jobType,
              orgAddress: this.form.orgAddress
            }
            addGradDirection(data).then(response => {
              this.$message({
                type: 'success',
                message: '成功添加考生号为: ' + this.form.id + ' 的毕业去向信息'
              })
              // 调用全局挂载的方法
              this.$store.dispatch('tagsView/delView', this.$route)
              // 返回上一步路由
              this.$router.push('/graduation/index')
            }).catch(err => {
              console.log(err)
            })
          } else {

            if(Array.isArray(this.form.origin)){
              let origin=this.form.origin[1]
            }else{
              origin=this.form.origin
            }
            //编辑上传
            let data = {
              id: this.form.id,
              sex: this.form.sex,
              nation: this.form.nation,
              politicsSt: this.form.politicsSt,
              school: this.form.school,
              eduBackgro: this.form.eduBackgro,
              major: this.form.major,
              origin: origin,
              gradTime: this.form.gradTime,
              difficulty: this.form.difficulty,
              department: this.form.difficulty,
              sid: this.form.sid,
              direction: this.form.direction,
              orgName: this.form.orgName,
              orgKind: this.form.orgKind,
              orgIndustr: this.form.orgIndustry,
              orgLocatio: this.form.orgLocation,
              jobType: this.form.jobType,
              orgAddress: this.form.orgAddress
            }
            editGradDirection(data).then(response => {
              this.$message({
                type: 'success',
                message: '成功编辑考生号为: ' + this.form.id + ' 的毕业去向信息'
              })
              // 调用全局挂载的方法
              this.$store.dispatch('tagsView/delView', this.$route)
              // 返回上一步路由
              this.$router.push('/graduation/index')
            }).catch(err => {
              console.log(err)
            })
          }
        } else {
          //提示有问题
          this.$message({
            message: '有必要信息未填写，请填写完整~',
            type: 'error'
          })
        }
      });

    },
    handleCancel() {
      this.$confirm('此操作将放弃表单所有内容，确定取消嘛？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用全局挂载的方法
        this.$store.dispatch('tagsView/delView', this.$route)
        // 返回上一步路由
        this.$router.push('/graduation/index')
      }).catch(() => {
        //do nothing
      });
    }
  }
}
</script>
